package leecode

func findMode(root *TreeNode) (result []int) {
	if root == nil {
		return
	}
	var nums []int
	var find func(node *TreeNode)
	find = func(node *TreeNode) {
		if node == nil {
			return
		}
		//左
		find(node.Left)
		nums = append(nums, node.Val)
		find(node.Right)
	}
	find(root)
	//统计数组中出现次数最多的元素
	var maxNum int
	mapNum := make(map[int]int)
	for i := 0; i < len(nums); i++ {
		_, ok := mapNum[nums[i]]
		if !ok {
			mapNum[nums[i]] = 1
		}
		mapNum[nums[i]] = mapNum[nums[i]] + 1
		if mapNum[nums[i]] > maxNum {
			maxNum = mapNum[nums[i]]
		}
	}
	for key, value := range mapNum {
		if value == maxNum {
			result = append(result, key)
		}
	}
	return
}
